package org.diy.traffic.sources;

import java.util.ArrayList;
import java.util.List;

import org.diy.traffic.updates.TrafficUpdate;

/**
 * This class is a stub representing the minimum amount of code required to create a custom TrafficSource. To be used for educational
 * purposes only, not really supposed to be instantiated.
 * @author Dan Greenblatt
 *
 */
public class TrafficSourceStub extends TrafficSource {

	public TrafficSourceStub() {
		//Must define a unique name for this instance of a TrafficSource and pass it to the base class
		//constructor. 
		
		super("STUB_SOURCE");
	}
	
	/**
	 * This function must be implemented; it is called by the InputDaemon and should do all the heavy lifting
	 * required to poll a source of traffic information for new data.
	 */
	@Override
	public List<TrafficUpdate> doCheck(boolean updateLastCheck)
			throws TrafficSourceException {
		
		// When this function is called, it is safe to assume that the InputDaemon has made sure that it's
		// indeed time to check this traffic source, so don't worry about checking for when i was last updated, etc.
		
		//Make an empty list of updates
		List<TrafficUpdate> myUpdates = new ArrayList<TrafficUpdate>();
		
		// Do a bunch of stuff that generates a list of TrafficUpdates
		for (int i=0; i<10; i++) {
			// the arguments are
			//   - a reference to the originating TrafficSource (i.e. this)
			//   - a timestamp for when the TrafficUpdate was generated (i.e. now)
			//   - an actual message containing info about the udpate
			TrafficUpdate t = new TrafficUpdate(this, System.currentTimeMillis(), "It's icy out there. Watch out!");
			
			// add a bit of extra info to the TrafficUpdates data object, to be consumed by the handling sink
			t.addData("EXTRA_INFO", new Object());
			myUpdates.add(t);
		}
	
		// if we're supposed to update the last time we were checked (based on function parameter), let's do this.
		if (updateLastCheck) {
			super.setLastCheckToNow();
		}
		
		// return the list of traffic updates to be put into the queue
		return myUpdates;
	}

}
